package cn.school.pay.service.impl;

import cn.school.common.utils.SysUserUtils;
import cn.school.pay.dao.ChargeItemDao;
import cn.school.pay.service.ChargeItemService;
import cn.school.pay.vo.ChargeItemVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ChargeItemServiceImpl extends ServiceImpl<ChargeItemDao, ChargeItemVO> implements ChargeItemService {

    @Override
    public IPage<ChargeItemVO> pageList(ChargeItemVO vo) {
        LambdaQueryWrapper<ChargeItemVO> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(vo.getChargeName())){
            wrapper.like(ChargeItemVO::getChargeName,vo.getChargeName());
        }
        if(!StringUtils.isEmpty(vo.getChargeAmount())){
            wrapper.eq(ChargeItemVO::getChargeAmount,vo.getChargeAmount());
        }

        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    @Override
    public int createOrUpdateCharge(ChargeItemVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        int num=0;
        if(StringUtils.isEmpty(vo.getId())){
            vo.setCreateBy(username);
            vo.setCreateTime(new Date());
            num = this.baseMapper.insert(vo);
        }else{
            vo.setModifyBy(username);
            vo.setModifyTime(new Date());
            num = this.baseMapper.updateById(vo);
        }
        return num;
    }

    @Override
    public int deleteById(List<Long> ids) {
        int num = 0;
        for (Long id:ids){
            this.baseMapper.deleteById(id);
            num++;
        }
        return num;
    }
}
